<head>
    <meta charset="UTF-8"> 
    <title>MyTool</title>
</head>
<body>
  <div class="_content">
      <form class="form-inline">
          <div class="form-group">
              <textarea type="text" class="from-control"
               id="_input" rows="6"  placeholder="请输入......"></textarea>
          </div>
      </form>
      <div>
          <button id="_button_encode" type="button" class="btn btn-primary"> 转码 </button>
          <button id="_button_decode" type="button" class="btn btn-primary"> 解码 </button>

      </div>
      <div class="form-group">
          <textarea type="text" class="from-control" id="_output" rows="5"></textarea>
      </div>
  </div>
  <script >
    function decode() {
      try {
          var inputValue = $("#_input").val().trim();
          var reg = /^['|"](.*)['|"]/;
          var responseParams = true;
          if (inputValue.includes("helloword")) {
              inputValue = inputValue.replaceAll("helloworld", "");
              responseParams = false;
          }
          inputValue = inputValue.replace(reg, "$1");
          var output = DECODE(inputValue);
          responseParams && output.res && (output.res = JSON.parse(output.res))
          $("#_output").val(JSON.stringify(output))
      } catch (error) {
          console.log(error); 
      }
    }
    function encode() {
      try {
          var inputValue = $("#_input").val().trim();
          var reg = /^['|"](.*)['|"]/;
          var responseParams = true;
          if (inputValue.includes("helloword")) {
              inputValue = inputValue.replaceAll("helloworld", "");
              responseParams = false;
          }
          inputValue = inputValue.replace(reg, "$1");
          var output = ENCODE(inputValue);
          responseParams && output.res && (output.res = JSON.parse(output.res))
          $("#_output").val(JSON.stringify(output))
      } catch (error) { 
          console.log(error);
      }
    }
    function ENCODE(input) { 
      //base64解密
      var base64 = btoa(input);
      //拼接
      var result = base64;
      return result; 
    }
    function DECODE(input) {
      //base64加密  
      var base64 =  atob(input);
      //拼接
      var result = base64;
      return result;  
    }

    document.addEventListener('DOMContentLoaded', () => {
      $("#_button_encode").click(() => {
          encode();
      })
      $("#_button_decode").click(() => {
          decode();
      })
    })
  </script>
</body> 